<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/image_data_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - image_data_layer.cpp<span style="font-size: 80%;"> (source / <a href="image_data_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">93</td>
            <td class="headerCovTableEntryLo">2.2 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">14</td>
            <td class="headerCovTableEntryLo">14.3 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #ifdef USE_OPENCV</a>
<span class="lineNum">       2 </span>            : #include &lt;opencv2/core/core.hpp&gt;
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &lt;fstream&gt;  // NOLINT(readability/streams)
<span class="lineNum">       5 </span>            : #include &lt;iostream&gt;  // NOLINT(readability/streams)
<span class="lineNum">       6 </span>            : #include &lt;string&gt;
<span class="lineNum">       7 </span>            : #include &lt;utility&gt;
<span class="lineNum">       8 </span>            : #include &lt;vector&gt;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #include &quot;caffe/data_transformer.hpp&quot;
<span class="lineNum">      11 </span>            : #include &quot;caffe/layers/base_data_layer.hpp&quot;
<span class="lineNum">      12 </span>            : #include &quot;caffe/layers/image_data_layer.hpp&quot;
<span class="lineNum">      13 </span>            : #include &quot;caffe/util/benchmark.hpp&quot;
<span class="lineNum">      14 </span>            : #include &quot;caffe/util/io.hpp&quot;
<span class="lineNum">      15 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">      16 </span>            : #include &quot;caffe/util/rng.hpp&quot;
<span class="lineNum">      17 </span>            : 
<span class="lineNum">      18 </span>            : namespace caffe {
<a name="19"><span class="lineNum">      19 </span>            : </a>
<span class="lineNum">      20 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      21 </span><span class="lineNoCov">          0 : ImageDataLayer&lt;Dtype&gt;::~ImageDataLayer&lt;Dtype&gt;() {</span>
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :   this-&gt;StopInternalThread();</span>
<span class="lineNum">      23 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      26 </span><span class="lineNoCov">          0 : void ImageDataLayer&lt;Dtype&gt;::DataLayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      27 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      28 </span><span class="lineNoCov">          0 :   const int new_height = this-&gt;layer_param_.image_data_param().new_height();</span>
<span class="lineNum">      29 </span><span class="lineNoCov">          0 :   const int new_width  = this-&gt;layer_param_.image_data_param().new_width();</span>
<span class="lineNum">      30 </span>            :   const bool is_color  = this-&gt;layer_param_.image_data_param().is_color();
<span class="lineNum">      31 </span>            :   string root_folder = this-&gt;layer_param_.image_data_param().root_folder();
<span class="lineNum">      32 </span>            : 
<span class="lineNum">      33 </span><span class="lineNoCov">          0 :   CHECK((new_height == 0 &amp;&amp; new_width == 0) ||</span>
<span class="lineNum">      34 </span>            :       (new_height &gt; 0 &amp;&amp; new_width &gt; 0)) &lt;&lt; &quot;Current implementation requires &quot;
<span class="lineNum">      35 </span>            :       &quot;new_height and new_width to be set at the same time.&quot;;
<span class="lineNum">      36 </span>            :   // Read the file with filenames and labels
<span class="lineNum">      37 </span>            :   const string&amp; source = this-&gt;layer_param_.image_data_param().source();
<span class="lineNum">      38 </span><span class="lineNoCov">          0 :   LOG(INFO) &lt;&lt; &quot;Opening file &quot; &lt;&lt; source;</span>
<span class="lineNum">      39 </span><span class="lineNoCov">          0 :   std::ifstream infile(source.c_str());</span>
<span class="lineNum">      40 </span>            :   string line;
<span class="lineNum">      41 </span>            :   size_t pos;
<span class="lineNum">      42 </span>            :   int label;
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :   while (std::getline(infile, line)) {</span>
<span class="lineNum">      44 </span>            :     pos = line.find_last_of(' ');
<span class="lineNum">      45 </span><span class="lineNoCov">          0 :     label = atoi(line.substr(pos + 1).c_str());</span>
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :     lines_.push_back(std::make_pair(line.substr(0, pos), label));</span>
<span class="lineNum">      47 </span>            :   }
<span class="lineNum">      48 </span>            : 
<span class="lineNum">      49 </span><span class="lineNoCov">          0 :   CHECK(!lines_.empty()) &lt;&lt; &quot;File is empty&quot;;</span>
<span class="lineNum">      50 </span>            : 
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :   if (this-&gt;layer_param_.image_data_param().shuffle()) {</span>
<span class="lineNum">      52 </span>            :     // randomly shuffle data
<span class="lineNum">      53 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Shuffling data&quot;;</span>
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :     const unsigned int prefetch_rng_seed = caffe_rng_rand();</span>
<span class="lineNum">      55 </span><span class="lineNoCov">          0 :     prefetch_rng_.reset(new Caffe::RNG(prefetch_rng_seed));</span>
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :     ShuffleImages();</span>
<span class="lineNum">      57 </span>            :   } else {
<span class="lineNum">      58 </span><span class="lineNoCov">          0 :     if (this-&gt;phase_ == TRAIN &amp;&amp; Caffe::solver_rank() &gt; 0 &amp;&amp;</span>
<span class="lineNum">      59 </span>            :         this-&gt;layer_param_.image_data_param().rand_skip() == 0) {
<span class="lineNum">      60 </span><span class="lineNoCov">          0 :       LOG(WARNING) &lt;&lt; &quot;Shuffling or skipping recommended for multi-GPU&quot;;</span>
<span class="lineNum">      61 </span>            :     }
<span class="lineNum">      62 </span>            :   }
<span class="lineNum">      63 </span><span class="lineNoCov">          0 :   LOG(INFO) &lt;&lt; &quot;A total of &quot; &lt;&lt; lines_.size() &lt;&lt; &quot; images.&quot;;</span>
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :   lines_id_ = 0;</span>
<span class="lineNum">      66 </span>            :   // Check if we would need to randomly skip a few data points
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :   if (this-&gt;layer_param_.image_data_param().rand_skip()) {</span>
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :     unsigned int skip = caffe_rng_rand() %</span>
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :         this-&gt;layer_param_.image_data_param().rand_skip();</span>
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Skipping first &quot; &lt;&lt; skip &lt;&lt; &quot; data points.&quot;;</span>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :     CHECK_GT(lines_.size(), skip) &lt;&lt; &quot;Not enough points to skip&quot;;</span>
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :     lines_id_ = skip;</span>
<span class="lineNum">      73 </span>            :   }
<span class="lineNum">      74 </span>            :   // Read an image, and use it to initialize the top blob.
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :   cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first,</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :                                     new_height, new_width, is_color);</span>
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :   CHECK(cv_img.data) &lt;&lt; &quot;Could not load &quot; &lt;&lt; lines_[lines_id_].first;</span>
<span class="lineNum">      78 </span>            :   // Use data_transformer to infer the expected blob shape from a cv_image.
<span class="lineNum">      79 </span><span class="lineNoCov">          0 :   vector&lt;int&gt; top_shape = this-&gt;data_transformer_-&gt;InferBlobShape(cv_img);</span>
<span class="lineNum">      80 </span><span class="lineNoCov">          0 :   this-&gt;transformed_data_.Reshape(top_shape);</span>
<span class="lineNum">      81 </span>            :   // Reshape prefetch_data and top[0] according to the batch_size.
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :   const int batch_size = this-&gt;layer_param_.image_data_param().batch_size();</span>
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :   CHECK_GT(batch_size, 0) &lt;&lt; &quot;Positive batch size required&quot;;</span>
<span class="lineNum">      84 </span><span class="lineNoCov">          0 :   top_shape[0] = batch_size;</span>
<span class="lineNum">      85 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; this-&gt;prefetch_.size(); ++i) {</span>
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :     this-&gt;prefetch_[i]-&gt;data_.Reshape(top_shape);</span>
<span class="lineNum">      87 </span>            :   }
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :   top[0]-&gt;Reshape(top_shape);</span>
<span class="lineNum">      89 </span>            : 
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :   LOG(INFO) &lt;&lt; &quot;output data size: &quot; &lt;&lt; top[0]-&gt;num() &lt;&lt; &quot;,&quot;</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :       &lt;&lt; top[0]-&gt;channels() &lt;&lt; &quot;,&quot; &lt;&lt; top[0]-&gt;height() &lt;&lt; &quot;,&quot;</span>
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :       &lt;&lt; top[0]-&gt;width();</span>
<span class="lineNum">      93 </span>            :   // label
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :   vector&lt;int&gt; label_shape(1, batch_size);</span>
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :   top[1]-&gt;Reshape(label_shape);</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; this-&gt;prefetch_.size(); ++i) {</span>
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :     this-&gt;prefetch_[i]-&gt;label_.Reshape(label_shape);</span>
<span class="lineNum">      98 </span>            :   }
<span class="lineNum">      99 </span><span class="lineNoCov">          0 : }</span>
<a name="100"><span class="lineNum">     100 </span>            : </a>
<span class="lineNum">     101 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     102 </span><span class="lineNoCov">          0 : void ImageDataLayer&lt;Dtype&gt;::ShuffleImages() {</span>
<span class="lineNum">     103 </span>            :   caffe::rng_t* prefetch_rng =
<span class="lineNum">     104 </span><span class="lineNoCov">          0 :       static_cast&lt;caffe::rng_t*&gt;(prefetch_rng_-&gt;generator());</span>
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :   shuffle(lines_.begin(), lines_.end(), prefetch_rng);</span>
<span class="lineNum">     106 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     107 </span>            : 
<span class="lineNum">     108 </span>            : // This function is called on prefetch thread
<span class="lineNum">     109 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     110 </span><span class="lineNoCov">          0 : void ImageDataLayer&lt;Dtype&gt;::load_batch(Batch&lt;Dtype&gt;* batch) {</span>
<span class="lineNum">     111 </span><span class="lineNoCov">          0 :   CPUTimer batch_timer;</span>
<span class="lineNum">     112 </span><span class="lineNoCov">          0 :   batch_timer.Start();</span>
<span class="lineNum">     113 </span>            :   double read_time = 0;
<span class="lineNum">     114 </span>            :   double trans_time = 0;
<span class="lineNum">     115 </span><span class="lineNoCov">          0 :   CPUTimer timer;</span>
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :   CHECK(batch-&gt;data_.count());</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :   CHECK(this-&gt;transformed_data_.count());</span>
<span class="lineNum">     118 </span><span class="lineNoCov">          0 :   ImageDataParameter image_data_param = this-&gt;layer_param_.image_data_param();</span>
<span class="lineNum">     119 </span><span class="lineNoCov">          0 :   const int batch_size = image_data_param.batch_size();</span>
<span class="lineNum">     120 </span><span class="lineNoCov">          0 :   const int new_height = image_data_param.new_height();</span>
<span class="lineNum">     121 </span><span class="lineNoCov">          0 :   const int new_width = image_data_param.new_width();</span>
<span class="lineNum">     122 </span>            :   const bool is_color = image_data_param.is_color();
<span class="lineNum">     123 </span>            :   string root_folder = image_data_param.root_folder();
<span class="lineNum">     124 </span>            : 
<span class="lineNum">     125 </span>            :   // Reshape according to the first image of each batch
<span class="lineNum">     126 </span>            :   // on single input batches allows for inputs of varying dimension.
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :   cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first,</span>
<span class="lineNum">     128 </span><span class="lineNoCov">          0 :       new_height, new_width, is_color);</span>
<span class="lineNum">     129 </span><span class="lineNoCov">          0 :   CHECK(cv_img.data) &lt;&lt; &quot;Could not load &quot; &lt;&lt; lines_[lines_id_].first;</span>
<span class="lineNum">     130 </span>            :   // Use data_transformer to infer the expected blob shape from a cv_img.
<span class="lineNum">     131 </span><span class="lineNoCov">          0 :   vector&lt;int&gt; top_shape = this-&gt;data_transformer_-&gt;InferBlobShape(cv_img);</span>
<span class="lineNum">     132 </span><span class="lineNoCov">          0 :   this-&gt;transformed_data_.Reshape(top_shape);</span>
<span class="lineNum">     133 </span>            :   // Reshape batch according to the batch_size.
<span class="lineNum">     134 </span><span class="lineNoCov">          0 :   top_shape[0] = batch_size;</span>
<span class="lineNum">     135 </span><span class="lineNoCov">          0 :   batch-&gt;data_.Reshape(top_shape);</span>
<span class="lineNum">     136 </span>            : 
<span class="lineNum">     137 </span><span class="lineNoCov">          0 :   Dtype* prefetch_data = batch-&gt;data_.mutable_cpu_data();</span>
<span class="lineNum">     138 </span><span class="lineNoCov">          0 :   Dtype* prefetch_label = batch-&gt;label_.mutable_cpu_data();</span>
<span class="lineNum">     139 </span>            : 
<span class="lineNum">     140 </span>            :   // datum scales
<span class="lineNum">     141 </span><span class="lineNoCov">          0 :   const int lines_size = lines_.size();</span>
<span class="lineNum">     142 </span><span class="lineNoCov">          0 :   for (int item_id = 0; item_id &lt; batch_size; ++item_id) {</span>
<span class="lineNum">     143 </span>            :     // get a blob
<span class="lineNum">     144 </span><span class="lineNoCov">          0 :     timer.Start();</span>
<span class="lineNum">     145 </span><span class="lineNoCov">          0 :     CHECK_GT(lines_size, lines_id_);</span>
<span class="lineNum">     146 </span><span class="lineNoCov">          0 :     cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first,</span>
<span class="lineNum">     147 </span><span class="lineNoCov">          0 :         new_height, new_width, is_color);</span>
<span class="lineNum">     148 </span><span class="lineNoCov">          0 :     CHECK(cv_img.data) &lt;&lt; &quot;Could not load &quot; &lt;&lt; lines_[lines_id_].first;</span>
<span class="lineNum">     149 </span><span class="lineNoCov">          0 :     read_time += timer.MicroSeconds();</span>
<span class="lineNum">     150 </span><span class="lineNoCov">          0 :     timer.Start();</span>
<span class="lineNum">     151 </span>            :     // Apply transformations (mirror, crop...) to the image
<span class="lineNum">     152 </span><span class="lineNoCov">          0 :     int offset = batch-&gt;data_.offset(item_id);</span>
<span class="lineNum">     153 </span><span class="lineNoCov">          0 :     this-&gt;transformed_data_.set_cpu_data(prefetch_data + offset);</span>
<span class="lineNum">     154 </span><span class="lineNoCov">          0 :     this-&gt;data_transformer_-&gt;Transform(cv_img, &amp;(this-&gt;transformed_data_));</span>
<span class="lineNum">     155 </span><span class="lineNoCov">          0 :     trans_time += timer.MicroSeconds();</span>
<span class="lineNum">     156 </span>            : 
<span class="lineNum">     157 </span><span class="lineNoCov">          0 :     prefetch_label[item_id] = lines_[lines_id_].second;</span>
<span class="lineNum">     158 </span>            :     // go to the next iter
<span class="lineNum">     159 </span><span class="lineNoCov">          0 :     lines_id_++;</span>
<span class="lineNum">     160 </span><span class="lineNoCov">          0 :     if (lines_id_ &gt;= lines_size) {</span>
<span class="lineNum">     161 </span>            :       // We have reached the end. Restart from the first.
<span class="lineNum">     162 </span>            :       DLOG(INFO) &lt;&lt; &quot;Restarting data prefetching from start.&quot;;
<span class="lineNum">     163 </span><span class="lineNoCov">          0 :       lines_id_ = 0;</span>
<span class="lineNum">     164 </span><span class="lineNoCov">          0 :       if (this-&gt;layer_param_.image_data_param().shuffle()) {</span>
<span class="lineNum">     165 </span><span class="lineNoCov">          0 :         ShuffleImages();</span>
<span class="lineNum">     166 </span>            :       }
<span class="lineNum">     167 </span>            :     }
<span class="lineNum">     168 </span>            :   }
<span class="lineNum">     169 </span><span class="lineNoCov">          0 :   batch_timer.Stop();</span>
<span class="lineNum">     170 </span>            :   DLOG(INFO) &lt;&lt; &quot;Prefetch batch: &quot; &lt;&lt; batch_timer.MilliSeconds() &lt;&lt; &quot; ms.&quot;;
<span class="lineNum">     171 </span>            :   DLOG(INFO) &lt;&lt; &quot;     Read time: &quot; &lt;&lt; read_time / 1000 &lt;&lt; &quot; ms.&quot;;
<span class="lineNum">     172 </span>            :   DLOG(INFO) &lt;&lt; &quot;Transform time: &quot; &lt;&lt; trans_time / 1000 &lt;&lt; &quot; ms.&quot;;
<span class="lineNum">     173 </span><span class="lineNoCov">          0 : }</span>
<a name="174"><span class="lineNum">     174 </span>            : </a>
<span class="lineNum">     175 </span>            : INSTANTIATE_CLASS(ImageDataLayer);
<a name="176"><span class="lineNum">     176 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(ImageData);</span></a>
<span class="lineNum">     177 </span>            : 
<span class="lineNum">     178 </span><span class="lineCov">          3 : }  // namespace caffe</span>
<span class="lineNum">     179 </span>            : #endif  // USE_OPENCV
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
